/*
 * @lc app=leetcode.cn id=236 lang=c
 *
 * [236] 二叉树的最近公共祖先
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */



struct TreeNode *lowestCommonAncestor(struct TreeNode *root, struct TreeNode *p, struct TreeNode *q)
{
    if (root == NULL || root == p || root == q)
    {
        return root;
    }
    struct TreeNode *res_left = lowestCommonAncestor(root->left, p, q);
    struct TreeNode *res_right = lowestCommonAncestor(root->right, p, q);

    if (res_left && res_right)
    {
        return root;
    }
    return res_left ? res_left : res_right;
}
// @lc code=end
